package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("region")
public class RegionController {

    @Autowired
    private IRegionService regionService;

    @Autowired
    private IDestinationService destinationService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") RegionQueryObject qo){

        Page<Region> page = regionService.query(qo);

        model.addAttribute("page", page);

        return "region/list";
    }

    @RequestMapping("/listDests")
    @ResponseBody
    public Object listDests(){

        List<Destination> list = destinationService.list();

        return list;
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){

        if (StringUtils.hasLength(region.getId())) {

            Region region1 = regionService.get(region.getId());
            if (region.getId() != null){
                region1.setId(region.getId());
            }
            if (region.getName() != null){
                region1.setName(region.getName());
            }
            if (region.getSn() != null){
                region1.setSn(region.getSn());
            }
            if (region.getInfo() != null){
                region1.setInfo(region.getInfo());

            }
                region1.setIshot(region.getIshot());
            if (region.getRefIds() != null){
                region1.setRefIds(region.getRefIds());
            }
                region1.setSequence(region.getSequence());

            regionService.update(region1);
        }else {
            regionService.save(region);
        }

        return JsonResult.success();
    }


    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){

        List<Destination> list = destinationService.queryDestByRegionId(rid);

        return list;
    }

    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot, String id){

        destinationService.updateIshot(hot, id);

        return JsonResult.success();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){

        regionService.delete(id);

        return JsonResult.success();
    }
}
